package com.feel.mall.goodsservice.controller;

import com.feel.mall.common.bean.Category;
import com.feel.mall.common.bean.RespBean;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.CategoryService;
import com.feel.mall.common.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询分类信息
     *
     * @return
     */
    @GetMapping()
    public RespBean getPageInfo(Category condition, Integer pageNum, Integer pageSize) {
        pageNum = pageNum != null && pageNum > 0 ? pageNum : 1;
        pageSize = pageSize != null && pageSize > 0 ? pageSize : 5;

        PageInfo<Category> pageInfo = (PageInfo<Category>) categoryService.getByCondition(condition, pageNum, pageSize);

        return RespBean.ok("查询成功", pageInfo);
    }

    /**
     * 新增分类信息
     */
    @PostMapping()
    public RespBean addInfo(@RequestBody Category category) throws FeelException {
        Boolean b = categoryService.add(category);
        return b ? RespBean.ok("新政成功") : RespBean.error();
    }

    /**
     * 更新分类信息
     */
    @PutMapping
    public RespBean updateInfo(@RequestBody Category condition) throws FeelException {
        Boolean b = categoryService.update(condition);
        return b ? RespBean.ok("更新成功") : RespBean.error();
    }

    /**
     * 删除分类信息
     */
    @DeleteMapping("/{id}")
    public RespBean delinfo(@PathVariable("id") Integer id) throws FeelException {
        Boolean b = categoryService.del(id);
        return b ? RespBean.ok("删除成功") : RespBean.error();
    }

    /**
     * 获取分类信息
     */
    @GetMapping("/{id}")
    public RespBean getCategory(@PathVariable("id") Integer id) {
        Category category = categoryService.getById(id);
        return RespBean.ok("查询成功", category);
    }

    /**
     * 获得全部分类信息
     */
    @GetMapping("/list/{id}/{status}")
    public RespBean getAllList(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        Category category = new Category();
        category.setParentId(id);
        if (!status.equals(1)) {
            status = null;
        }
        category.setStatus(status);
        List<Category> list = (List<Category>) categoryService.getByCondition(category, null, null);
        return RespBean.ok("查询成功", list);
    }
}
